﻿using DAL.DataProvider;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace DAL
{
    public class ChiTietHoaDonDAO
    {
        public void InsertChiTietHoaDon(ChiTietHoaDon ctHoaDon)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO ChiTietHoaDon VALUES (@MaHD,@MaHang,@SoLuong,@GiaGoc,@VAT,@GGHang)", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaHD", ctHoaDon.maHoaDon);
            cmd.Parameters.AddWithValue("@MaHang", ctHoaDon.maHang);
            cmd.Parameters.AddWithValue("@SoLuong", ctHoaDon.soLuongMua);
            cmd.Parameters.AddWithValue("@GiaGoc", ctHoaDon.giaTruocVAT);
            cmd.Parameters.AddWithValue("@VAT", ctHoaDon.VAT);
            cmd.Parameters.AddWithValue("@GGHang", ctHoaDon.maGiamGiaHang);
            cmd.ExecuteNonQuery();
        }

        public void UpdateChiTietHoaDon(ChiTietHoaDon ctHoaDon)
        {
            SqlCommand cmd = new SqlCommand("UPDATE ChiTietHoaDon SET SoLuongMua=@SoLuong" +
                " WHERE MaHang=@MaHang and MaHoaDon=@MaHD", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaHD", ctHoaDon.maHoaDon);
            cmd.Parameters.AddWithValue("@MaHang", ctHoaDon.maHang);
            cmd.Parameters.AddWithValue("@SoLuong", ctHoaDon.soLuongMua);
            cmd.Parameters.AddWithValue("@GiaGoc", ctHoaDon.giaTruocVAT);
            cmd.Parameters.AddWithValue("@VAT", ctHoaDon.VAT);
            //cmd.Parameters.AddWithValue("@GGHang", ctHoaDon.maGiamGiaHang);
            cmd.ExecuteNonQuery();
        }

        public void DeleteChiTietHoaDon(ChiTietHoaDon ctHoaDon)
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM ChiTietHoaDon WHERE MaHoaDon=@MaHD and MaHang=@MaHang", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaHD", ctHoaDon.maHoaDon);
            cmd.Parameters.AddWithValue("@MaHang", ctHoaDon.maHang);
            cmd.ExecuteNonQuery();
        }

        public List<ChiTietHoaDon> GetListChiTietHoaDon()
        {
            List<ChiTietHoaDon> list = new List<ChiTietHoaDon>();
            SqlCommand cmd = new SqlCommand("select * from ChiTietHoaDon", DataAccess.GetConnection());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ChiTietHoaDon obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = new ChiTietHoaDon();
                obj.maHoaDon = dt.Rows[i]["MaHoaDon"].ToString();
                obj.maHang = dt.Rows[i]["MaHang"].ToString();
                obj.soLuongMua = Convert.ToInt32(dt.Rows[i]["SoLuongMua"].ToString());
                obj.giaTruocVAT = Convert.ToInt32(dt.Rows[i]["GiaTruocVAT"].ToString());
                obj.VAT = Convert.ToInt32(dt.Rows[i]["VAT"].ToString());
                obj.maGiamGiaHang = dt.Rows[i]["MaGiamGiaHang"].ToString();
                list.Add(obj);
            }
            return list;
        }

        public DataTable SelectAllChiTietHoaDon(string maHD)
        {
            SqlCommand cmd = new SqlCommand("select * from view_ChiTietHoaDon where MaHoaDon=@maHD", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@MaHD", maHD);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public ChiTietHoaDon DataRowToChiTietHoaDon(DataTable dt)
        {
            ChiTietHoaDon obj = new ChiTietHoaDon();
            obj.maHoaDon = dt.Rows[0]["MaHoaDon"].ToString();
            obj.maHang = dt.Rows[0]["MaHang"].ToString();
            obj.soLuongMua = Convert.ToInt32(dt.Rows[0]["SoLuongMua"].ToString());
            obj.giaTruocVAT = Convert.ToInt32(dt.Rows[0]["GiaTruocVAT"].ToString());
            obj.VAT = Convert.ToInt32(dt.Rows[0]["VAT"].ToString());
            obj.maGiamGiaHang = dt.Rows[0]["MaGiamGiaHang"].ToString();
            return obj;
        }


        public DataView SelectViewChiTietHoaDon(string maHD)
        {
            DataView dv = new DataView(this.SelectAllChiTietHoaDon(maHD));
            return dv;
        }
    }
}
